const util = require('../../../utils/util.js')
const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain
const parameter = getApp().globalData.parameter
const jg_stu_id = wx.getStorageSync('jg_stu_id') || ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_url:oss_domain + '/static/picture/app_icon/',
    // capacity_list:['全部',10,50,100,150,200,250,300,350,400,450,500],
    capacity_list:[100, 200, 300, 400,500],
    capacity:100,
    test_duration:30, // 时间
    chap_id_list:[],
    chap_list:[{name:'第一章 无人机概述'},{name:'第二章 系统组成及介绍'},{name:'第三章 飞行原理与飞行性能'},{name:'第四章 航空气象'},{name:'第五章 空中交通管制'},{name:'第六章 无人机驾驶员起降阶段操纵技术'},{name:'第七章 无人机驾驶员巡航阶段操纵技术及相关知识'},{name:'第八章 旋翼机'},{name:'第九章 综合问答'},{name:'第十章 教员法（教员适用）'},],
    time_list:[10,20,30,40,50,60,70,80,90,100,110,120],
  },


  // 查看成绩单
  view_transcript_list(e){
    getApp().get_vibration()
    var test_type = e.currentTarget.id  // 1-班内统考；2-自定义考试
    wx.navigateTo({
      url: '/pages/doTi/transcript_list/transcript_list?jg_stu_id=' + jg_stu_id + '&stu_name=我&test_type=' + test_type,
    })


  },


  // 选择题量
  set_capacity(e){
    let index = e.detail.value
    this.setData({
      capacity:this.data.capacity_list[index]
    })
  },




  // 设置考试时长
  set_test_duration(e){
    // console.log('设置考试时长',e.detail.value)
    this.setData({
      test_duration:this.data.time_list[e.detail.value]
    })
  },


  // 提交
  submit(e){
    getApp().get_vibration()
    if(jg_stu_id){
      if(this.data.chap_id_list.length>0){
          wx.showModal({
            content: '如果中途退出，本次测试作废',
            showCancel: true,
            title: '提示',
            success: res => {
              if(res.confirm){
                wx.showLoading({
                  title: '创建试卷中……',
                })
                wx.request({
                  url: domainName + '/custom_testPapers_create',
                  data: {
                    test_type:2,  // 2-自定义考试
                    jg_stu_id:jg_stu_id,
                    chap_id_list:JSON.stringify(this.data.chap_id_list),
                    test_duration:this.data.test_duration,
                    capacity:this.data.capacity,
                  },...parameter,
                  success: res => {
                    let test_paper_id = res.data.test_paper_id
                    if(test_paper_id){
                      wx.redirectTo({
                        url: '/pages/doTi/do_test/do_test?test_paper_id=' + test_paper_id
                      })
                    }else{
                      wx.showModal({
                        content: '试卷创建失败，请稍后重试。',
                        showCancel: false,
                        title: '提示',
                      })
                    }
                    wx.hideLoading()
                  },
                })
              }
            },
          })
      }else{
        wx.showModal({
          content: '请选择考试范围',
          showCancel: false,
          title: '提示',
        })
      }
    }else{
      wx.showModal({
        content: '您不是在训学员，或尚未开通权限',
        editable: false,
        showCancel: false,
        title: '提示',
        success: res => {
          getApp().apply_get_right()
        },
      })
    }
  },



  //  选择章节
  chap_id_list(e){
    if(!this.data.chap_list[0].id){
      wx.showModal({
        content: '仅在训学员可用!',
        showCancel: false,
        title: '提示',
      })
      return
    }
    // console.log('选择考试范围',e)
    let chap_id_list = e.detail.value
    this.setData({
      chap_id_list:chap_id_list
    })
  },

  // 选择全部章节
  all_chap(e){
    if(!this.data.chap_list[0].id){
      wx.showModal({
        content: '仅“在训学员”可用!',
        showCancel: false,
        title: '提示',
      })
      return
    }
    var chap_list = this.data.chap_list
    var chap_id_list = this.data.chap_id_list
    for(var i=0;i<chap_list.length;i++){
      var chap_id = chap_list[i].id
      if(chap_id){
        chap_id = chap_id.toString()
        let has_index = chap_id_list.indexOf(chap_id)
        if(has_index>-1){
          chap_id_list.splice(has_index,1)
        }else{
          chap_id_list.push(chap_id)
        }
      }
    }
    this.setData({chap_id_list})
  },


  // 获取用户信息
  page_init(jg_stu_id){
    wx.showLoading()
    wx.request({
      url: domainName + '/custom_testPapers_init',
      data: {
        jg_stu_id,
      },...parameter,
      success: res=> {
        wx.hideLoading()
        let chap_list = res.data.chap_list
        if(chap_list.length>0){this.setData({chap_list})}
          // console.log('res',res.data)
        this.setData({
          avatar:res.data.avatar,
          nick_name:res.data.nick_name,
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      test_date:util.formatDate(new Date()),
    })
    this.page_init(jg_stu_id)
  },

})